﻿using MyAlbum.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System;
using System.Linq;
using MyAlbum.Model;
using PersonalAlbum.Exceptions;
namespace UnitTests
{
    
    
    /// <summary>
    ///This is a test class for TagEntryServiceTest and is intended
    ///to contain all TagEntryServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class TagEntryServiceTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        [TestInitialize()]
        public void MyTestInitialize()
        {
            _testConfig = new TestConfig();
            tags = new List<MyAlbum.Model.Tag>();
            entries = new List<Entry>();
        }
        //
        //Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
            foreach (var item in tags)
            {
                Service.DeleteTagName(item.Name);
            }
            _testConfig.Dispose();
            foreach (var item in entries)
            {
                EntryService.DeleteItem(item);
            }
        }
        //
        #endregion
        private TestConfig _testConfig;
        private List<MyAlbum.Model.Tag> tags;
        public MyAlbum.Services.TagEntryService Service
        {
            get
            {
                return ObjectFactory.GetInstance<ITagEntryService>() as MyAlbum.Services.TagEntryService;
            }
        }

        private List<MyAlbum.Model.Entry> entries;
        MyAlbum.Services.EntryService EntryService
        {
            get
            {
                return ObjectFactory.GetInstance<IEntryService>() as EntryService;
            }
        }

        /// <summary>
        ///A test for GetTags
        ///</summary>
        [TestMethod()]
        public void GetTagsTest()
        {
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            MyAlbum.Services.TagEntryService target = this.Service;
            Entry e = EntryServiceTest.Create();
            this.EntryService.AddItem(e, new MyAlbum.Utility.GenericValidationSummary());
            this.entries.Add(e);
            var actual = target.GetTags();
            Assert.IsNotNull(actual);
            foreach (var item in e.Tags)
            {
                Assert.IsNotNull(actual.SingleOrDefault(t => t.Name == item.Name));
            }
        }

        /// <summary>
        ///A test for DeleteTagName
        ///</summary>
        [TestMethod()]
        public void DeleteTagNameTest()
        {
            var user = new GenericUser("tal", new string[] { "Admin" });
            ((TestUser)ObjectFactory.GetInstance<IUser>()).SetUser(user);
            MyAlbum.Services.TagEntryService target = this.Service;
            Entry e = EntryServiceTest.Create();
            this.EntryService.AddItem(e, new MyAlbum.Utility.GenericValidationSummary());
            this.entries.Add(e);
            var actual = target.GetTags();
            Assert.IsNotNull(actual);
            foreach (var item in e.Tags)
            {
                Assert.IsNotNull(actual.SingleOrDefault(t => t.Name == item.Name));
                target.DeleteTagName(item.Name);
            }

            actual = target.GetTags();
            Assert.IsNotNull(actual);
            foreach (var item in e.Tags)
            {
                Assert.IsNull(actual.SingleOrDefault(t => t.Name == item.Name));
            }
        }

        /// <summary>
        ///A test for TagEntryService Constructor
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(AssertionException))]
        public void TagEntryServiceConstructorTest()
        {
            MyAlbum.Data.ITagEntryRepository repository = null; 
            MyAlbum.Services.TagEntryService target = new MyAlbum.Services.TagEntryService(repository);
        }
    }
}
